package com.controller;

import com.common.ApiRestResponse;
import com.common.Result;
import com.filter.UserFilter;
import com.pojo.Cart;
import com.service.CartService;
import com.vo.CartVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/shopcar")
public class CartController {
    //判断用户是否登录 若没登录 返回未登录信息 提醒登录
    @Autowired
    CartService cartService;

    @PostMapping("/add")
    public Result add(@RequestBody Cart cart) {

        System.out.println(cart);
        int add = cartService.add(cart);
        System.out.println(add);
        return new Result(200, "ok", add);
    }

    @DeleteMapping("/del/{id}")
    public Result del(@PathVariable("id") int id) {

        int del = cartService.del(id);
        return new Result(200, "ok", del);
    }

    @GetMapping("/selectall")
    public Result select() {

        List<Cart> carts = cartService.select();
        return new Result(200, "ok", carts);
    }

    @PostMapping("/upadd/{id}")
    public Result upadd(@PathVariable("id") int id) {
        int i = cartService.upadd(id);
        return new Result(200, "ok", i);

    }

    @PostMapping("/upjian/{id}")
    public Result upjian(@PathVariable("id") int id) {

        int i = cartService.upjian(id);
        return new Result(200, "ok", i);
    }

    /**
     * 显示购物车表
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("购物车列表")
    public ApiRestResponse list() {
        //内部获取用户ID，防止横向越权
        List<CartVO> cartList = cartService.list(UserFilter.currentUser.getId());
        return ApiRestResponse.success(cartList);
    }

    /**
     * 测试 添加购物车
     * @param productId
     * @param count
     * @return
     */
    @PostMapping("/addCart")
    @ApiOperation("添加商品到购物车")
    public ApiRestResponse add(@RequestParam Integer productId, @RequestParam Integer count) {
        List<CartVO> cartVOList = cartService.add(UserFilter.currentUser.getId(), productId, count);
        return ApiRestResponse.success(cartVOList);
    }
}
